package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Locale;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
     private NursingLevelMapper nursingLevelMapper;

    /**
     * 分页查询
     * @param name
     * @param page
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse page(String name, Integer page, Integer pageSize, Integer status) {
        PageHelper.startPage(page, pageSize);
        Page<NursingProjectVo> page0=nursingProjectMapper.selectByPage(name, status);
        //设置绑定被护理等级绑定的个数
        page0.forEach(o->{
            o.setCount(nursingLevelMapper.selectCountByPlanId(o.getId()));
        });
        return PageResponse.of(page0, NursingProjectVo.class);
//        return PageResponse.of(page0.getTotal(),page0.getResult());
    }

    @Override
    public void add(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.insert(nursingProject);
    }

    @Override
    public NursingProjectVo selectById(Long id) {
        NursingProjectVo  nursingProjectVo= nursingProjectMapper.selectById(id);
        nursingProjectVo.setCount(nursingLevelMapper.selectCountByPlanId(id));
        return nursingProjectVo;
    }

    @Override
    public void update(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    public void enableOrDisable(Long id, Integer status) {
        NursingProject nursingProject = NursingProject.builder().id(id).status(status).build();
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    public void delete(Long id) {
        nursingProjectMapper.delete(id);
    }

    @Override
    public List<NursingProjectVo> getAll() {
       List<NursingProjectVo> list= nursingProjectMapper.getAll();
        return list;
    }

}
